/*
 * @Author: fishroom
 * @Date: 2022-10-14 10:28:08
 * @LastEditTime: 2022-10-26 10:45:50
 * @FilePath: \koa\app\server\Sys\SysUserServer.ts
 */
import { Op } from "sequelize";
import sys_user from "../../model/sys_user";
class SysUserServer {
  async getUserList(limit: number = 10, offset: number = 0, params: any) {
    let option = {
      where: {
        [Op.and]: [params.user_id],
      },
      offset,
      limit,
    };
    if (params.username)
      option.where[Op.and].push({
        username: {
          [Op.substring]: params.username,
        },
      });
    return await sys_user.findAndCountAll(option);
  }
  async updateUserById(user: any) {
    if (!user.user_id) return new Error("没有id");
    let user_id = user.user_id;
    delete user.id;
    user.role_id = user.roleIdList;
    delete user.roleIdList;
    sys_user.update(
      { ...user },
      {
        where: {
          user_id,
        },
      }
    );
  }
  async deleteUserById(user_ids: Array<number>) {
    if (!user_ids) return;
    sys_user.destroy({
      where: {
        user_id: {
          [Op.or]: user_ids,
        },
      },
    });
  }
  async addUser(user: any) {
    if (user.user_id) delete user.user_id;
    user.role_id = user.roleIdList;
    delete user.roleIdList;
    sys_user.create(
      { ...user },
      { fields: ["username", "password", "salt", "email", "mobile", "role_id"] }
    );
  }
}
export default new SysUserServer();
